package com.cloudlead.fleetAction.service.impl;
import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.BaseFleetCustomerService;
import com.cloudlead.commonData.service.TbFleetCostService;
import com.cloudlead.commonData.service.TbFleetOrderService;
import com.cloudlead.commonData.vo.*;
import com.cloudlead.fleetAction.service.CustomerBillSer;
import com.cloudlead.fleetService.vo.CustomerBillRebVo;
import com.cloudlead.fleetService.vo.CustomerBillVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by Will on 2015/12/24.
 */
@Service
public class CustomerBillSerImpl implements CustomerBillSer {
    @Autowired
    TbFleetCostService tbFleetCostService;
    @Autowired
    TbFleetOrderService tbFleetOrderService;
    @Autowired
    BaseFleetCustomerService baseFleetCustomerService;
    @Override
    public CustomerBillRebVo findAll(CustomerBillVo customerBillVo, int rows, int page) {
        PageRequest pageRequest = new PageRequest();
        List<Term> list = new ArrayList<Term>();
        list.add(new Term("flag","0"));
        if(null != customerBillVo.getBaseCompanyId() && !"".equals(customerBillVo.getBaseCompanyId())){
            TbFleetOrder tbFleetOrder = new TbFleetOrder();
            BaseCompany baseCompany = new BaseCompany();
            baseCompany.setId(customerBillVo.getBaseCompanyId());
            tbFleetOrder.setFleetCompany(baseCompany);
            List<TbFleetOrder> list1 = tbFleetOrderService.list(tbFleetOrder,null);
            List list2 = new ArrayList<>();
            for(TbFleetOrder tbFleetOrder1:list1){
                list2.add(tbFleetOrder1.getId());
            }
            list.add(new Term("baseFleetOrder.id", Operator.IN,list2));
        }
        if(null != customerBillVo.getOrderType() && !"".equals(customerBillVo.getOrderType())){
            list.add(new Term("sort",customerBillVo.getOrderType()));
        }
        if(null != customerBillVo.getCustomer() && !"".equals(customerBillVo.getCustomer())){
            TbFleetOrder tbFleetOrder = new TbFleetOrder();
            BaseFleetCustomer baseFleetCustomer = new BaseFleetCustomer();
            baseFleetCustomer.setId(customerBillVo.getCustomer());
            tbFleetOrder.setCustomerCo(baseFleetCustomer);
            List<TbFleetOrder> list1 = tbFleetOrderService.list(tbFleetOrder,null);
            List list2 = new ArrayList<>();
            for(TbFleetOrder tbFleetOrder1:list1){
                list2.add(tbFleetOrder1.getId());
            }
            list.add(new Term("baseFleetOrder.id",Operator.IN,list2));
        }
        if(null != customerBillVo.getOrderBeginDate() && !"".equals(customerBillVo.getOrderBeginDate())){
            list.add(new Term("baseFleetOrder.createTime",Operator.GT,customerBillVo.getOrderBeginDate()));
        }
        if(null != customerBillVo.getOrderEndDate() && !"".equals(customerBillVo.getOrderEndDate())){
            list.add(new Term("baseFleetOrder.createTime",Operator.LT,customerBillVo.getOrderEndDate()));
        }
        if(null != customerBillVo.getCostName() && !"".equals(customerBillVo.getCostName())){
            list.add(new Term("costCode",customerBillVo.getCostName()));
        }
        Filter filter  = new Filter(new Criteria(list));
        pageRequest.setFilter(filter);
        pageRequest.setSize(rows);
        pageRequest.setPage(page);
        Page page1 = tbFleetCostService.findAll(pageRequest);
        CustomerBillRebVo customerBillRebVo = new CustomerBillRebVo();
        List<TbFleetCost> list1 = page1.getContent();
        List<Map> list2 = new ArrayList<Map>();
        for(TbFleetCost tbFleetCost : list1){
            Map map = new HashMap<>();
            map.put("id",tbFleetCost.getId());
            map.put("bussinessCode",tbFleetCost.getBaseFleetOrder()==null?"":tbFleetCost.getBaseFleetOrder().getBussinessCode());
            map.put("packagingAddress",tbFleetCost.getBaseFleetOrder()==null?"":tbFleetCost.getBaseFleetOrder().getPackagingAddress());
            map.put("removeAddress",tbFleetCost.getBaseFleetOrder()==null?"":tbFleetCost.getBaseFleetOrder().getRemoveAddress());
            map.put("costCode",tbFleetCost.getCostCode());
            map.put("costName",tbFleetCost.getCostName());
            map.put("money",tbFleetCost.getMoney());
            map.put("calculateCount",tbFleetCost.getCalculateCount());
            map.put("createTime", tbFleetCost.getBaseFleetOrder()==null?"":tbFleetCost.getBaseFleetOrder().getCreateTime());
            BaseFleetCustomer baseFleetCustomer = baseFleetCustomerService.view(tbFleetCost.getBaseFleetOrder() == null ? "" : tbFleetCost.getBaseFleetOrder().getCustomerCo().getId());
            map.put("contactCompany",baseFleetCustomer==null?"":baseFleetCustomer.getCompanyShortName());
            list2.add(map);
        }
        customerBillRebVo.setTotal(page1.getTotalPages());
        customerBillRebVo.setRows(list2);
        return customerBillRebVo;
    }
}
